Can I put a bmw 540i 2002 engine on my 525i 2002
With all the right parts and lots of money it should be possible. Be aware that BMW probably also had a different suspension for the 540.
You can, but you'll need a new wiring harness, transmission and steering rack (recirculating ball) to start. Also, getting a reliable m62tu is not cheap, unless you want to risk it with a junk yard engine. There are quite a few suspension differences including struts/shocks as Full_of_Regrets mentioned.