The $h$ in $mgh$ can be measured from any arbitrary point - the absolute value of the gravitational potential energy doesn't have much meaning, what's important is the $\textbf{change}$ in gravitational potential energy between two points in a gravitational field. With reference to the diagram, I can add an arbitrary constant offset $A$ to how I label my $h$ values, but the change in gravitational potential energy is unchanged as it only depends on the $\textbf{difference}$ in $h$ values at the two positions.
If I drop an object from rest at height $h_2$ and measure its velocity at height $h_1$, I'd be using the $\textbf{Conservation of Energy}$ equation,
$\frac{1}{2} m v^2 = mg(h_2-h_1)$
If I do the same thing, but using a convention where the heights measured are now shifted up by $A$, i.e. the object is dropped from height $h_2+A$ and its velocity is measured at height $h_1+A$, we still get an identical conservation of energy equation. Note that the upper and lower levels still correspond to the same positions in space, I'm just using a different label for their positions.
To give a tangible example, in many cases, it's useful to measure the height from the ground level, so implicitly we're saying that $h=0m$ occurs at the ground level. Let's say that there's a box whose top surface is 5m above the ground level, so we label its position as $h=5m$. The gain in gravitational potential energy when in object is moved from the ground onto the top of the box is then $mg(5-0) = 5mg$. If instead we choose to measure the zero reference on the box's surface, i.e. the box's surface corresponds to $h=0m$, the ground must correspond to $h=-5m$ since it is 5m below the box's surface. The gain in gravitational potential energy when in object is moved from the ground onto the top of the box is then $mg(0-(-5)) = 5mg$ and is still the same as before.
The equation $m_2gh - m_1gh = \frac{1}{2} (m_1 + m_2)v^2$ is just a conservation of energy equation, the left-hand-side expressing the net loss in gravitational potential energy, which is converted into kinetic energy, expressed on the right-hand-side. If I'm not mistaken I think the masses are swapped, $m_2gh$ represents the gravitational potential energy lost by $m_2$ as it moved downwards and $m_1gh$ represents the gravitation potential energy gained by $m_1$ as it moved upwards, so the net amount of gravitational potential energy converted to kinetic energy should be $m_2gh - m_1gh$.