Some background: In Lagrangian mechanics, to obtain the EL equations, one varies the action (I will be dropping the time dependence since I don't think it's relevant) $$S[q^i(t)] = \int dt \, L(q^i, \dot q^i) $$ and deduces the EL equations from the principle of least action as well as the typical restriction on the variation. One then learns that the Lagrangian formalism is heavily restrictive in terms of what coordinate transformations it allows for (it only allows for point-transformations) and so one seeks a description of mechanics in which what we have are truly independent $2n$ coordinates. These $2n$ coordinates are of course the generalized positions and canonical momenta, whereby one inverts the equation $$\frac{\partial L}{\partial \dot q^i} = p_i$$ to obtain $\dot q^i = \dot q^i (q^j, p_j)$ which is possible when $L$ is a convex function of the velocities, then one performs a Legendre transform of the Lagrangian to obtain the Hamiltonian $H$. To actually obtain the EOMs by means of a variational principle and without using the EL equations, one varies the action $$S = \int dt \, p_i \dot q^i - H(q^i, p_i)$$ by varying $q$ and $p$ independently. My concern is, why do we have to do all this? Why can't one just invert the definition of canonical momenta and plug that back into the Lagrangian, then just vary action with the Lagrangian as a function $L = L(q, p)$? This clearly gives incorrect results, but I don't feel like I understand why it was allowed for us to do that in the case of the Hamiltonian but not in the case of the Lagrangian. It seems the issue has to do with the presence of a time derivative of the positions which allows us to integrate by parts, but I still don't feel like I understand this and I hope somebody could clear my confusion. EDIT: I suppose my question just boils down to why we need to take a Legendre transform at all. I understand the need to have coordinates such that we could vary them independently, but I don't understand why the Legendre transform is the right (or only?) way to do this.
EDIT: I do \emph{now} see the motivation for Legendre transforms, however I am now even more confused as to why what I attempted to do in writing $S = \int dt \, L(q, p, t)$ and varying $q$ and $p$ independently is wrong, and I don't really see an (explicit) answer to this in the question this was deemed a duplicate to.