This is a callback to Frodo's earlier meeting with Gildor Inglorion, where a Black Rider is approaching (and seems about to find) the Hobbits, but is disturbed by the arrival of the Elves:
O Elbereth! Gilthoniel!
We still remember, we who dwell
In this far land beneath the trees,
Thy starlight on the Western Seas
It's notable that even before the Elves arrive, Frodo is aware of the name Elbereth and what it signifies:
'These are High Elves! They spoke the name of Elbereth!' said Frodo in amazement...
At Frodo's parting from Gildor, following a discussion of the Black Riders (and Gildor's reluctance to divulge much information about them) we also see the following:
'Is it not enough to know that they are servants of the Enemy?' answered Gildor. 'Flee them! Speak no words to them! They are deadly. Ask no more of me! But my heart forbodes that, ere all is ended, you, Frodo son of Drogo, will know more of these fell things than Gildor Inglorion. May Elbereth protect you!'
It's also notable that Frodo invokes Elbereth again at the Ford of Bruinen when trying to stand off the Riders:
'By Elbereth and Lúthien the Fair,' said Frodo with a last effort, lifting up his sword, 'you shall have neither the Ring nor me!'
Finally, we see that Frodo is here also using the name of Lúthien, which follows on from Aragorn's earlier summary of the tale of Beren and Lúthien (before the attack on Weathertop).
The connection is therefore that Frodo is using names from tales he has heard and encounters he has had along the way. He knows from the encounter with the High Elves (Gildor & co) that the name Elbereth has some power, he has Gildor's blessing that Elbereth protect him, and he has recent memory of the tale of Lúthien.