How in Lagrangian and Hamiltonian mechanics we take derivatives with respect to velocity and momentum respectively if they are vectors? Can we take derivative with respect to a vector?
Strictly speaking we can only take a "covariant" derivative of a vector, for which we need to use a Affine connection (Christoffel symbols) for particular manifold/coordinate system we are doing physics on.
In the flat space and Cartesian coordinates the Christoffel symbols are identically zero. Thus regular derivatives could be taken and are automatically covariant.
In the curvilinear coordinates or on a curved manifold, it is not immediately obvious that Lagrange equations are covariant. However, it can be shown (see some differential geometry textbook, like one by Frankel) that they are covariant.