The hostile creature imposes disadvantage regardless
Aiming a ranged attack is more difficult when a foe is next to you. When you make a ranged attack with a weapon, a spell, or some other means, you have disadvantage on the attack roll if you are within 5 feet of a hostile creature who can see you and who isn’t incapacitated.
The "Ranged Attacks in Close Combat" rule does not let the hostile creature choose whether it wants to disrupt the ranged attacker. This rule assumes that the hostile creature always does its best to be hostile in combat, especially because this rule is risk-free. This is true even if the hostile creature wants to go unnoticed1.
Another way to look at it is that this rule defines a behavior common to all hostile creatures. If a DM wants a hostile creature to behave differently, they have two options: rule against RAW or rule that the creature is not hostile.
- Ruling against RAW is totally fine, as long as you know what you're doing. I can't think of any far-reaching (or even close-reaching) consequences to ruling against RAW in this situation.
- Ruling that a creature is not hostile comes with its own implications, so I do not recommend this course of action unless there are other compelling reasons.
1. The "Ranged Attacks in Close Combat" and "Hiding" rules do not conflict.
The closest thing to "I'm unaware of it and it doesn't make any attempt to make itself known" is "It's hidden from me and wants to stay hidden".
If you are hidden—both unseen and unheard—when you make an attack, you give away your location when the attack hits or misses.
Whether the hostile creature is unseen, unheard, or both is of no consequence for the the "Ranged Attacks in Close Combat" rule. Moreover, the hostile creature is unseen because it is invisible and does not need to make attacks of its own to disrupt the ranged attacker.
The only question is whether the hostile creature can disrupt the ranged attacker silently. At first glance this may seem farfetched, but it can still be narrated plausibly. Lets consider two examples of silent disruptions:
- Messing with the bow string, when the ranged attacker fires so that the shot is weak. The attacker assumes he didn't draw enough the first time, so he readjusts and that messes up his other attacks too.
- Knocking an arrow away just before the ranged attacker fires. The attacker believes his fingers slipped and he fumbles through the following attacks.
Is it difficult for the hostile creature to perform those disruptions silently? Does it require good timing and a distracted ranged attacker? Yes, yes, and yes! But our assumption is that the hostile creature is hidden from the ranged attacker, which means they already had a perception vs stealth contest that accounts for these interactions.