I've been chasing this rattle for years on my 2003 WRX Sedan. I've tried all the fixes posted from the various subaru boards from layering foam between the headliner and metal roof... WD-40 along the rear windshield rubber surround....silicone the plastic clips along B and C pillars...etc.
Here's the symptoms. Random ticking or tapping which seems to come from the rear drivers side roof area. It's so annoying because it's right behind my ear. It goes away in the winter but extremely loud during summer temps. Putting pressure on the headliner with my hand right behind my head while driving silences the noise. So naturally I thought it was the headliner.
I was so annoyed, I took the entire headliner out of the car to troubleshoot. There was still noise so it's not the headliner. I thought it was the boxy plastic structures along the b-pillar but holding them with my hand while driving does not help.
After a while I figured out it was the black metal support bracket pictured here:
In detail here:
As you can see, there's lots of residual foam tape from other attemps at silencing this noise.
You have to pry off the plastic boxy supports to access this bracket. I don't have the energy to go step by step...but you guys can figure it out
The noise was coming from the bracket rubbing or vibrating against the structure it was bolted on to. When the car is moving, the vibration from the chasis would make it tap metal against metal. Either it just didn't fit well or the bolts were a little loose. I didn't take any chances. I took it off and put felt tape all along the metal contact surfaces.
Put the bolts back on LOOSLY and shake it a bit to see if it makes noise. Add padding where necessary to quiet the noise. Then tighten once satisfied.
Hope this saves someone some agony or searching for rattle that seems to come from the headliner. While you have the body parts off, it would be a good idea to see what else rattles and make noise. I put felt tape on various surfaces to limit the amount of rattles from the c and b pillars.
I did this for the passenger side also just in case it gets rattling in the future.