There are a lot of good answers to this question, but I'd like to give an answer from the perspective of the proof. Your method works because the proof ensures that it works. You can read the simple proof here on this wiki page to finding the solution $f$ of the exact differential equation
$M(x,y)dx + N(x,y)dy = 0 $
They give that the solution $f$ is $ f(x,y) = \int M(x,y) \; dx + g(y)$ $f(x,y) = \int M(x,y) \;dx + \Big(\int N(x,y) \;dy - \int \frac{\partial}{\partial y}\int M \; dx \;\;dy \Big) $
In other words to be clear, the solution is not $ f = \int M \;dx + \int N \; dy$
We aren't adding on a full $\int N \; dy$ to $\int M\;dx$. Rather we are adding on a $\int N\; dy - \int \frac{\partial}{\partial y} \int M \; dx \;\;dy$. Your idea of merging terms, is more like (stated another way), adding on the terms which aren't already present in $\int M\; dx$. To understand this, we have to understand why $\int \frac{\partial }{\partial y}\int M\;dx \; \;dy$ is subtracting off terms in $\int N \; dy$ already present in the other integral of $M$, leaving "unique terms."
Let
$ M(x,y) = a(x) + b(y) + c(x,y) + d\\ N(x,y) = e(x) + f(y) + g(x,y) + h $
where $c$ and $g$ represent functions whose terms contain both $x$ and $y$ together simultaneously (like $x^2y + x\sin(y)$). [Also, excuse my use of the letter $f$ again when I already designated $f$ the solution to the differential equation! I didn't recognize this repeated use of the letter until the very end of this answer- it shouldn't pose problems]. Using $\frac{\partial M}{\partial y} = \frac{\partial N}{\partial x}$, you get
$ b'(y) + \frac{\partial c}{\partial y}(x,y) = e'(x) + \frac{\partial g}{\partial x}(x,y)$
The only way for this to be true is if $b'(y) = \frac{\partial g}{\partial x}$ and $e'(x) = \frac{\partial c}{\partial y}$. Or
$g(x,y) = \int b'(y) \; dx = x b'(y) + u(y)\tag{1}$
$c(x,y) = \int e'(x) \; dy = y e'(x) + v(x)\tag{2}$
(I will drop these two integration constants - explained at the end of this paragraph. Also I will not add in any integrating constants to the integrals below. If you study the proof in the link above, you will see that the integrating constant to $\int M dx$ is in fact the term $\int (N - \frac{\partial}{\partial y}\int M dx) \;\;dy$. It's already been added so we don't need to account for it again. The only integrating constant that the proof missed was a $+c$ at the very end when they integrated $g'(y)$ to get $g(y)$. The reason why I'm dropping the integrating constants $u(y)$ and $v(x)$ is because a $u(y)$ would already be apart of $f(y)$ and any $v(x)$ would be apart of $a(x)$ - see the meaning of $g$ and $c$ above)
Let's write out the integrals $ \int M \; dx = \int a(x) \;dx + \underbrace{xb(y)} + \fbox{$\int c(x,y)\; dx$} + xd$
$ \int N \; dy = \fbox{$y e(x)$} + \int f(y) \; dy + \underbrace{\int g(x,y) \; dy} + yh$
Due to $(1)$ above, the under-braces are matching terms. Due to $(2)$ above, the boxed terms are matching terms. Therefore looking at the form of the solution $f$, once we have all the terms in $\int M\; dx$, I claim that it's only the "unique terms in $\int N\; dy$ " that are added on. I show this by showing that $\int \frac{\partial}{\partial y}\int M\; dx \;\;dy$ subtracts out the boxed and underbraced terms in $\int N \; dy$, leaving only the unique terms.
From $\int M\;dx$,
$\frac{\partial}{\partial y}\int M\; dx = x b'(y) + \frac{\partial}{\partial y}\int c(x,y) \; dx $
And
$\int \frac{\partial}{\partial y}\int M \;dx \; \; dy = xb(y) + \int c(x,y) \; dx \\ = \underbrace{\int g(x,y) \; dy} + \fbox{$y e(x)$} $
Therefore our solution $f(x,y) = \int M(x,y) \;dx + \Big(\int N(x,y) \;dy - \int \frac{\partial}{\partial y}\int M \; dx \;\;dy \Big) $
Is given by the "merging"
$ f = \int a(x) \; dx + xb(y) + \int c(x,y) \; dx + xd + \underbrace{\int f(y) \; dy + yh}_{\text{unique terms added on}}$
(excuse again my use of $f$ standing for the solution of the differential equation and $f$ standing for a component of $N(x,y)$). You can check that $\partial/\partial x$ of this equation gives $a + b + c + d = M$ and $\partial /\partial y$ gives $e + f + g + h = N$