I have done this. I have "upgraded" a 2001 325xi to a 330xi using an M54B30 from a 2004 vehicle. The M54 engines are robust despite the E46 cooling system design shortcomings. I present some caveats:
1) Yes, you have to reflash the DME after you get the car started. Be careful. There is a limit on the number of times you can do that.
2) You will need to replace all of the air intake including the box and hoses, throttle body, injectors, intake manifold, DISA Valve, and maybe even the coils. Use this time to replace your fuel filter if you haven't already. The tank-mounted fuel pump will thank you. There are two vacuum-related components mounted on the rear of the engine/intake manifold. One is a small pump and the other a sensor I believe. I replaced them just in case.
3) Depending on the donor car, you may need to modify your engine harness so you don't throw misfire codes. This involves adding a resistor in the ground circuit of the injector wiring.
4) Plan on replacing all gaskets (intake manifold, oil pan, valve cover, exhaust, oil dipstick tube, oil filter housing), all vacuum lines (suggest going silicone with those)
5) You'll need to consider your engine extraction and transmission strategy. Some take everything out without removing the front of the car. Great if you have high ceilings where you're working and goes faster. Some take the tranny with the engine and remove the entire front end of the vehicle. I did this since I wanted to inspect the torque converter and I needed to replace the flywheel with the 3.0 liter version. CAUTION: I re-used the 2.5l auto transmission. It will work with the 3.0l engine but your shift points will be slightly off. If you're a highway driver, you'll hardly notice. If you are trying to create a performance car, you'll need to upgrade your tranny to the 3.0l GM auto or the 5- or 6-speed manual while you have everything apart. 50s Kid on YouTube in my opinion has the best auto to manual E46 swap video out there. 2nd place goes to Jason at Shoplife TV.
6) Now's a good time to consider replacing your starter, alternator, water pump, A/C compressor, and power steering pump while everything is out. If nothing else consider the starter motor strongly. It's notoriously complex to get out of an E46 chassis (one of the bolts is a bear to access).
7) DO NOT re-use torque-to-yield bolts when you remove the subframe, engine mounts, or transmission from the engine. Basically be suspect of anything M8 and larger and verify the type and follow the torque specifications when re-assembling your car. There are probably other components/fasteners I'm omitting so if in doubt use RealOEM or a Bentley manual to guide you. Some of the bolts/fasteners are only available as BMW-spec. You'll have to bite the bullet on those but know that in doing so your car is less likely to fall apart under "spirited driving." Boltdepot.com is another great source. I purchased high temp stainless steel fasteners for the exhaust that way. No way I was going to spend $70 for the exhaust pipe to cat interface bolts again.
8) REPLACE all the plastic cooling components and lines, especially those that are hidden up underneath the intake manifold. These are notorious for slow leaks that lead to overheating and head gasket failure. REPLACE your crankcase vent valve and the associated hoses. Use new O-rings and lubricate before installing them. Clean the ports on the engine prior to installation. Use Pentofrost NF or BMW OEM blue coolant when you re-fill the system and follow the cumbersome bleed process for the E46 to get all the air out of the system.
9) Consider changing out your engine and transmissions mounts (plus differential mount if it's an XI).
10) Same for the VANOS oil lines depending on their age
11) Somewhere along the line of the E46's life the A/C compressor went from a 3-bolt mount to a 4-bolt mount. You'll need that adapter plus the hardware depending on the year of the donor engine.
12) I did the manual transmission electric fan swap conversion when I did the engine swap. It's really convenient to service the car this way but bear in mind that the DME will NOT WARN YOU if this fan fails. This is why the automatic cars had both an electrically- and a a belt-driven fan. I test the fan after every oil change and swap it every 45,000 miles just in case.
I'm attaching the parts list I created when I did the swap. You don't have to do everything I did. The car had 183k miles on it when the head gasket blew. I just replaced every major component that I hadn't already because I'm keeping the car for another 15+ years. But don't skimp on seals and gaskets. Replace those regardless of how old they are or the last time you replaced them. One of the reasons I shop at FCP Euro. Lifetime parts guarantee. Use RealOEM.com or another parts finder to make sure you're verifying fitment based on your replacement engine.
Good luck and let us know if we can help you!