I want to use a single GPIO pin as optocoupled input and output (at random discretion). Will the following scheme work for any possible combination of input and output? Especially when optocoupler 1 is open but GPIO output set to low?
This could work, but whenever the input optocoupler is turned on, the output optocoupler will also turn on (assuming that the current from the first is sufficient to turn the other optocoupler on. Even if the current is not high enough, the LED of the second will be 'slightly' on, which will allow current through the other side of the optocoupler, the CPC1018N only needs ~0.25mA of LED current to turn on.)
It's not worth it to gain an extra gpio. If you need an extra gpio there are better ways to get one than trying to worry about current through an optoisolator.
By definition, you can't use a digital pin as an output pin and also as an input pin at the same time.
Suppose the output circuitry in the pin is trying to drive a given logic level but the external input circuitry wants the pin to be read as the opposite logic level. The input circuitry would have to source or sink a great deal of current to overdrive the output circuitry, and even if that actually happened it would mean that the "output pin" is no longer at its correct logic level.
Most processors will allow you to reconfigure a pin as input or output on the fly, but you can't have a logic '1' and a logic '0' on the same pin at the same time.
The problem is dealing with (or avoiding) collisions.
To deal with collisions you need one output for your TX state, and one simultaneously-active RX input to detect when the TX data you sent doesn't match. You would implement a protocol on top of that to deal collisions though retries, acknowledgements and data integrity checks.
If you have control over the TX and RX events - for instance, the other devices don't send data until they're asked (polled) for it, then you don't need to detect collisions and your approach might work.