I have an Audi A4 1998 with over 200k miles. It gets extremely noisy when idle. It usually doesn't happen until it has been running for a while. Revving above 2000rpm can settle it and it isn't noisy when moving. Sometimes, if the car has been idling and noisy for a while, the oil light will flash and beep but goes away once moving again (there is enough oil). The noise is a very loud rattling. What is it and how can I go about fixing it?
The reason the oil light is coming on is because the oil pressure in the engine does fall low enough for the oil pressure switch to trigger. After all, it's a pressure warning light and not an oil level warning light. It's not unknown for these switches to go bad but given that it's accompanied by engine noise, I would very much work on the assumption that you have an oil pressure problem.
Get a mechanic to check the oil pressure with a known accurate gauge and you'll probably find that idle oil pressure is too low. Generally that would be a sign of engine wear, but under certain circumstances it can be caused by oil that's way too thin for the engine. Either way, the cause of the noises might already have caused enough damage for you to need to have the engine looked at.
Possible low oil pressure. Could be due to, or currently in the process of causing damage to the main bearings.
I had a similar situation with my Toyota. Apparently the oil pressure light comes on only at VERY low oil pressure. The car was down around that value at idle, and at speed it was up just barely above it (enough to keep the light off, but was gradually doing extensive engine damage) due to a failing oil pump. It ended up costing me a $4500 engine rebuild eventually after we exhausted all the simple "fixes" (bad oil pressure sensor, etc). Main bearings were trashed, cylinder bores were trashed (had to overbore the engine to save the block), etc.
I recommend taking it in and having the oil pressure checked by a professional ASAP! Wouldn't hurt to take a borescope to it, and possibly drop the oil pan to check the bearings (I assume they're readily accessible on an Audi, like they are on other makes?).
If this is the 2.8L engine, I'd be willing to bet that it's lifters. I have a '97 A4 2.8 quattro and around 140K miles one morning when I went to start it I heard a rattling. I immediately took it over to our mechanic, less than a mile away. It was not accompanied by an oil pressure light that I recall, but I really didn't drive it very long, either...
However, I believe they redesigned the engine shortly after our 1997 to have 4 instead of 2 valves per cylinder, so your engine may be different from ours. In our case, we spent a couple of grand for new lifters and camshaft. If I had it to do over again, I probably would have seriously considered getting a used engine on ebay, especially with a car over 200K miles (since the car isn't worth so much any more).
Here's a photo of the one lifter that was worn entirely through the top: