From the Super Mario Wiki
The Coin Ring is an underwater object appearing in Super Mario 64 and its remake, Super Mario 64 DS. They are seen only in the ninth course of the game, Dire, Dire Docks. When Mario swims through a coin ring, it will replenish one wedge of his health meter, and a number will appear above Mario. This number represents the amount of coin rings Mario has passed through. When Mario swims through five coin rings, he will be granted with a Power Star.
Coin rings were required for Stars 4 and 5 in Dire, Dire Docks. In Star 4, "Through the Jet Stream", Mario must swim to the other side of the tunnel to find a jet stream spraying out coin rings. After Mario collects five of these coin rings, a Power Star appears above the jet stream that Metal Mario can collect. In Star 5, "The Manta Ray's Award", Mario will find a Manta Ray swimming around underwater and leaving a trail of coin rings. Mario must follow this trail to collect five coin rings and make the Power Star appear.
They also appear in Super Mario 3D Land, where they look similar to Red Rings but with a yellow color. If passed through, they give five coins when passed through. The same Coin Rings also appear in Super Mario 3D World, except they only give three coins each.