Intuition about non-invariance of the Hamiltonian in canonical transformation Suppose $q={\{q_i\}}_{i=1}^n$ is the set of generalized coordinates of a dynamical system. $L(q,\dot q,t)$ is the Lagrangian of the system. Now we make coordinate transformation $Q_i=Q_i(q,t)$. Then the new Lagrangian,
$$\tilde L(Q_i,t)=L(q_i(Q,t),\dot q_i(Q,\dot Q,t),t).$$
The above equation comes by just change of variables.
And the new Lagrange's equations are just $$\frac{d}{dt}\frac{\partial\tilde L}{\partial\dot Q_i}-\frac{\partial\tilde L}{\partial Q_i}=0.$$
Now coming to Hamiltonian formulation. We make phase space transformation. Let $H(q_i,p_i,t)$ be the Hamiltonian of the system. $$Q_i=Q_i(q,p,t)\text{  and  }P_i=P_i(q,p,t).$$
But these transformations does not preserve the form of the Hamilton's equations of motion. There are only small subset of phase space transformation (canonical transformations) which preserves the form of Hamilton's equation of motion but with a new Hamiltonian $$K(Q,P,t)=H(q_i(Q,P,t),p_i(Q,P,t),t)+\frac{\partial G}{\partial t}$$
where $G$ is the generating function of the canonical transformation.
I want to know that in Hamiltonian why can't we directly apply change of variables as we have done in Lagrangian?
I have seen answers to the related question in this site, but they use heavy mathematics and also use differential geometry to explain the reasoning.
I am beginner to classical mechanics. I have not studied these advanced mathematics.
May someone please explain the reason in somewhat simpler terms, so that I will get some intuition.
The related question which is given in the comments by Qmechanic, I have read it already. But it uses lots of differential geometry and advance mathematical concepts. But I am beginner to theoretical classical mechanics. That's why I want to get some intuitive idea about it.
 A: *

*There are more variables and hence more coordinate transformations in the Hamiltonian formulation as compared to the Lagrangian formulation. So in order to give a fair/honest comparison, we only analyze coordinate transformations that originate from the Lagrangian formulation in this answer.


*In the Lagrangian formulation, the Lagrangian $L$ is invariant under general time-dependent coordinate transformations
$$ Q^k~=~f^k(q,t) \tag{1}$$
of the underlying configuration manifold $M$.


*Note that the velocity
$$ \frac{dQ^k}{dt}~=~\frac{\partial f^k(q,t)}{\partial q_j}\frac{dq^j}{dt}+\frac{\partial f^k(q,t)}{\partial t} \tag{2}$$
does not transform covariantly as a (1,0) tensor under time-dependent coordinate transformations (1).


*In the Hamiltonian formulation, the corresponding canonical transformation (CT) [which extends (1)] in phase space [i.e. the cotangent bundle $T^{\ast}M$]  is
$$ Q^k~=~f^k(q,t), \qquad p_j~=~\frac{\partial f^k(q,t)}{\partial q_j} P_k. \tag{3}$$
One may show that the CT (3) is a symplectomorphism, i.e it preserves the Poisson bracket. The CT (3) is generated by a generator
$$F_2(q,P,t)~=~f^k(q,t)P_k \tag{4}$$
of type 2.


*Note that the momentum (3) does transform covariantly as a (0,1) tensor under time-dependent coordinate transformations (1).


*The Hamiltonian and Kamiltonian are defined via Legendre transformation
$$ H~=~p_k\frac{dq^k}{dt} - L, \qquad 
K~=~P_k\frac{dQ^k}{dt} - L. \tag{5}$$


*The fact that the Hamiltonian is not invariant
$$ K-H~=~\frac{\partial F_2(q,P,t)}{\partial t}  
~=~\frac{\partial f^k(q,t)}{\partial t} P_k \tag{6}$$
under the CT (3) can be traced back to the non-covariance of eq. (2) in case of explicit time dependence. This answers OP's question.


*It goes without saying that the Hamiltonian is also not invariant under more general phase space transformations, cf. e.g. this Phys.SE post.
