Bidirectional driving solenoid using H bridge and raspberry pi
The basic idea is OK, though the implementation isn't.  You need diodes across each of the transistors (cathode to collector) instead of the TVS. You need resistors in series with each of the bases, and if the GPIO only puts out 3.3V you would want a 5V level translator. You might be better off using P-Channel MOSFETs for the top devices and N-Channel FETs on the low side.  Then the body diodes in the FETs will take care of the inductive current.  (Use logic level FETs.)

You don't need opto-isolators, but isolation might be a good idea since it will protect your GPIOs in case of problems during debug.  Your switching speed (20Hz) is low enough that you don't need super fast drive.

Without isolation and separate supplies you will need to hook the two grounds together.

If you use FETs, large value pull-down resistors from gate to source are not a bad idea. If you use opto-couplers depending on the type you may need additional circuitry to interface to the FETs or bipolars correctly.