In a Lagrangian, why can't we replace kinetic energy by total energy minus potential energy?

OP is essentially asking:

Why can't we replace the Lagrangian $L=T-V$ with $L=E-2V$ by using energy conservation $T+V=E$, where $E$ is an integration constant?

Answer: Generically an action principle gets destroyed if we apply EOMs in the action.

Specifically, OP used energy conservation $T+V=E$, which were derived from EOMs. Here it is important to understand that the stationary action principle must be defined for all (sufficiently smooth) paths. Not just the classical trajectories, which satisfy the EOMs. Note in particular, that the off-shell/virtual paths are not required to obey energy conservation.

Alternatively, it is easy to check that OP's proposed Lagrangian $L=E-2V$ would lead to wrong EOMs.

For examples, see this & this related Phys.SE posts.


Of course you can write $L=E-2V$, but you have to be careful with what is the total energy $E$ of the system. In the expression of the Lagrangian, the kinetic, potential, and total energy are considered to be defined instantaneously. The total energy is

$$E=\frac12m\dot x^2+mgx$$

You wrote instead that $E=mgh$. This is not the instantaneous total energy, but the initial total energy.