Change the random variable for calculating the expectation

The low of unconscious statistician (LOTUS) reads:

$E[u(Z)]=\int dz f_Z(z)u(z)$

By LOTUS on the right applied with $u(Z) \rightarrow h \circ g(X)$ you have $E[h(g(X))]$.

On the left instead always by LOTUS applied on $u(Z)\rightarrow h(Y)$ you have $E[h(Y)]$.

Since $Y=g(X)$ you are evaluating the same expectation written in two different ways and therefore the identity holds.