As you can well see, it is geometrically impossible! The curve (rail) in some places is very narrow, and the section curve (rectangle, in your case) can not curve perfectly, if not auto-intersect.
Relying on Rhino to guess where your surfaces should be all along your twisting single rail is likely to fail like you described.
My first suggestion is to copy your profile curve and use the orient tools to place it everywhere you want it to be in a specific orientation.
When you do the sweep, make sure the seam line follows the appropriate point of your profile so it doesn’t twist over itself.
You also have a bigger problem that your rail has some tight turns that force your profile curve to self-intersect. That will never give satisfactory results.
Either make the curves in your rail bigger, or make special smaller profile curves that will sweep cleanly without self-intersecting.