Optimal balance for rotating shallow water in primitive variables