My potatoes are near harvest and dying back, although one of them seems to have a rotting stem. I looked in the soil and found this?
Most likely it is a moth pupa. There are many species of moth that have similar looking pupae.
Possibly the caterpillar damaged your potato stem and rot invaded the damaged part.
The pupa itself is harmless, though the moth that hatches from it and the caterpillars the come from the next generation of eggs might not be!
You might want to harvest (and eat) the damaged potatoes early, to avoid the rot spreading into the tubers and potentially damaging more of the crop if you plan to store the potatoes.