This would violate the Object Interaction Rules
In 5th Edition D&D, you're granted 1 free object interaction per turn, limited to only simple interactions. Opening your mouth probably counts (to make the object visible) and closing it probably also counts (to make it obscured) but doing both in the same turn would require you to use the Use an Object Action.
It might seem strange, but mechanically, it's not requiring (substantially) more effort than lifting said rock out of your pocket + putting it back.
A Thief archetype Rogue might have an easier time of it
Thief Archetype Rogues gain the Fast Hands feature at level 3, permitting them to perform the Use an Object Action as a Bonus Action instead. So theoretically, as a Thief Rogue (multiclassed with whatever class gives you Spellcasting abilities), you could
- Close your mouth (Free Object Interaction)
- Cast a Spell or make an Attack (Action)
- Open your mouth (Bonus Action)
You could also do something less disgusting, like putting the rock inside a latched box that you then open + close using this sequence.
Some DMs are particular about the Simultaneity of Combat
Because the separation of combat into distinct "turns" is meant to be an abstraction of real-time combat, some DMs interpret this as meaning that it's not possible to meaningfully "strobe" an effect like this without some delayed interaction (i.e. a Reaction used on someone else's turn). The degree to which this is RAW is hotly contested, but this is a common interpretation of the rules.
So as a result, some DMs may rule that this is what actually happens:
- You close your mouth (Free Object Interaction)
- The Darkness effect may or may not vanish, depending on what happens before your turn ends
- You make an attack or cast a spell. If you open your mouth later in this turn, the attack is made with Disadvantage, or the spell must not require visibility of the target
- If you open your mouth the Darkness effect doesn't vanish.
- If you instead choose not to open your mouth, the Darkness effect does vanish (becomes obscured by your mouth) at the time you close your mouth (???)
Again: I am not certain that is a RAW ruling. I'm just informing you that that is a ruling many DMs make.
Human[oid] biology is disgusting
Strictly RAW, the Darkness effect spreads around corners, and so long as there is an unbroken path to all points within its radius, the Darkness spreads to that point. So some DMs may rule that the inside of your mouth (which is connected to your nostrils, even your ears to a degree, through your windpipe...) does not adequately obscure the Darkness effect. This may vary depending on the creature's race, but 5e doesn't describe in precise detail how the anatomy of other Humanoid races vary from the anatomy of humans.