Replace optoisolator with a resistor
No, using a 10M&Omega; resistor will make the output voltage collapse: The slightest current you draw on the output will cause a big voltage drop across this 10M&Omega; resistor.

If you're sure the optoisolator only isolates for noise and not for safety, ground loops, etc, you can either directly connect the 5V Square Wave input signal as the output or, when the signal is too weak, replace the whole circuit by a unity gain buffer amplifier.