In a Lagrangian, why can't we replace kinetic energy by total energy minus potential energy? TL;DR: Why can't we write $\mathcal{L} = E - 2V$ where $E = T + V = $ Total Energy?
Let us consider the case of a particle in a gravitational field starting from rest.
Initially, Kinetic energy $T$ is $zero$ and Potential energy $V$ is $mgh$.
At any time $t$, Kinetic energy $T = \frac{m\dot x^2}{2}$ and Potential energy $V$ is $mgx$.
$$\mathcal{L} = T-V = \frac{m\dot x^2}{2}-mgx.$$
If we write $T = mgh-mgx$, the Lagrangian becomes $\mathcal{L} = T-V = mgh-2mgx$ which is independent of $\dot x$ . Here $\frac{d}{dt}\frac{\partial \mathcal{L}}{\partial \dot x} = 0$ while $\frac{\partial \mathcal{L}}{\partial x} = -2mg$.
Why does this simple change of form of Lagrangian not work?
I do understand that this form does not have $\dot x$ but what is the deeper reason for this to not work?
How do I know that my Lagrangian is correct (for any arbitrary problem)?
 A: 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.
A: 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.
A: In deriving the eqns of motion from the principle of least action, position and velocity are considered independent variables
